<?php
class tejiaControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$url=APPSHOP."?m=tejia";
		$limit=40;
		$start=get('per_page','i');
		$where=" shopid=".SHOPID." AND tablename='shop' AND is_temp=0 ";
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("tejia")->select($option,$rscount);
		$this->smarty->assign(array(
			"data"=>$data
		));
		$this->smarty->display("tejia/index.html");
	}
	
	public function onAdd(){
		$id=get_post('id','i');
		if($id){
			$row=M("tejia")->selectRow("id=".$id);
		}
		if(empty($row)){
			$row=M("tejia")->selectRow("shopid=".SHOPID." AND tablename='shop' AND is_temp=1");
			if(empty($row)){
				M("tejia")->insert(array(
					"shopid"=>SHOPID,
					"dateline"=>time(),
					"is_temp"=>1,
					"siteid"=>SITEID,
					"tablename"=>"shop"
				));
				$row=M("tejia")->selectRow("shopid=".SHOPID." AND is_temp=1");
			}
		}
		$this->smarty->assign(array(
			"data"=>$row
		));
		$this->smarty->display("tejia/add.html");
	}
	
	public function onSave(){
		$starttime=strtotime(post('starttime'));
		$endtime=strtotime(post('endtime'));
		$data=M("tejia")->postData();
		$data['starttime']=$starttime;
		$data['endtime']=$endtime;
 
		$id=get_post('id','i');
		$row=M("tejia")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID){
			$this->goAll("暂无权限",1);
		}
		if($id){
			$data['is_temp']=0;
			M("tejia")->update($data,"id=".$id);
		}else{
			$data['table']='shop';
			M("tejia")->insert($data);
		}
		
		$this->goAll("保存成功");
	}
	
}

?>